home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6779 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Fast Fields?
  5. Date: 14 Feb 1996 18:40:14 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4fu6eeINNm4a@keats.ugrad.cs.ubc.ca>
  8. References: <4fqj6v$i71@hermes.louisville.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4fqj6v$i71@hermes.louisville.edu>,
  12. Alan Wild <arwild01@homer.louisville.edu> wrote:
  13. >Actually I'm more concerned with convienent.  I am currently coding a
  14. >small routine to parse a space delimted text file, but I only need to
  15. >keep field #6 of 8.  I know I could simply declare temporary variables
  16. >to store the extra functions and fscanf my way through the file, but is
  17. >there an alternative?
  18. >
  19. >Basically I'm lookng for some of the functionality of perl/awk in a
  20. >C routine so that I don't have to deal with dummy variables and whatnot.
  21. >
  22. >BTW, This project has to be done in C.  This is only a small piece of
  23. >the whole puzzle, but an essential piece nonetheless.
  24. >
  25. >Any thoughts?
  26.  
  27. Try learning how to use the ``lex'' lexical analyzer generator. Though you
  28. don't write a lex specification in C (well, not exactly), the resulting program
  29. that it writes for you is C.
  30.  
  31. In a nutshell, you give lex a bunch of patterns to match associated with C
  32. snippets which it will perform when matches happen. Lex uses the spec to
  33. compiler a fast, table-driven textfile snarfer that blows the doors off scanf()
  34. and friends for anything non-trivial. 
  35.  
  36. You won't quite get the high-level functionality of awk. The concept of
  37. pattern/action pairs may sound similar to awk, but it isn't, exactly. For one
  38. thing, only one pattern can match at a time (under normal operation).
  39. Secondly, the input is treated as a stream of characters, not lines. Patterns
  40. that are matched are extracted from the stream.
  41.  
  42. I almost always use lex together with yacc, even for parsing data files that
  43. have trivial grammars.
  44.  
  45.  
  46. These tools may be overkill for what you are doing, but once you master lex,
  47. you will find yourself using it for even trivial jobs like extracting space
  48. separated fields from a data file. The lex spec is a lot easier to maintain
  49. than hand-written scanning code.
  50.  
  51. If you want to do it quickly, it's probably best to avoid scanf. Buffer entire
  52. lines and scan through them using quick loops to look for spaces and extract
  53. what you want.  Or build a state machine which uses fgetc() to read individual
  54. characters, and returns tokenized fields.
  55.  
  56. -- 
  57.  
  58.